package com.ExcelServer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ExcelServer.client.PointsCriteriaClient;
import com.ExcelServer.client.TeacherInfoClient;
import com.ExcelServer.common.Result;
import com.ExcelServer.common.ResultDetail;
import com.ExcelServer.common.TimeWordsList;
import com.ExcelServer.exception.ChangeStatusException;
import com.ExcelServer.mapper.PerformanceTrendsMapper;
import com.ExcelServer.pojo.entity.PerformanceTrends;
import com.ExcelServer.pojo.entity.PointsCriteria;
import com.ExcelServer.pojo.form.add.AddDetailsForm;
import com.ExcelServer.pojo.form.add.CreateTableForm;
import com.ExcelServer.pojo.form.delete.DeleteDetailsForm;
import com.ExcelServer.pojo.form.update.UpdateDetailsForm;
import com.ExcelServer.pojo.form.update.UpdateStatusForm;
import com.ExcelServer.pojo.form.update.UpdateTableForm;
import com.ExcelServer.pojo.query.SelectQuery;
import com.ExcelServer.pojo.query.TableNameQuery;
import com.ExcelServer.pojo.query.TableQuery;
import com.ExcelServer.pojo.vo.*;
import com.ExcelServer.service.PerformanceTrendsService;
import com.ExcelServer.util.BaseTableUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author admin
 * @description 针对表【performance_trends(业绩的动态表格控制器)】的数据库操作Service实现
 * @createDate 2024-03-23 11:45:48
 */
@Service
public class PerformanceTrendsServiceImpl extends ServiceImpl<PerformanceTrendsMapper, PerformanceTrends> implements PerformanceTrendsService {

    @Autowired
    PerformanceTrendsMapper performanceTrendsMapper;

    @Autowired
    BaseTableUtil baseTable;
    @Autowired
    PointsCriteriaClient pointsCriteriaClient;
    @Autowired
    TeacherInfoClient teacherInfoClient;


    /**
     * 动态 构造 sql 语句
     **/
    @Override
    public Result createTable(CreateTableForm createTableForm) {
        if(createTableForm.getTableName().equals("")){
            return Result.fail("表格名称不能为空");
        }

        //构造Map描述
        //后端列名和前端列名对应器
        Map<String, String> property = new HashMap<>();

        //构造存储信息的动态业绩实例
        PerformanceTrends performanceTrends = new PerformanceTrends();

        //构造crete的sql 的条件器
        StringBuilder columnDefinition = new StringBuilder();

        Integer maxId = performanceTrendsMapper.getMaxId();
        String MTableName = "PTable0";
        if (maxId != null) {
            MTableName = "PTable" + maxId;
        }
        performanceTrends.setMTableName(MTableName);

        //获取默认值值-基础列信息
        List<Map<String, String>> columns = baseTable.getColumns();

        //获取前后端列名对应值-基础列的前后端信息
        List<Map<String, String>> fmColumnsMap = baseTable.getFMColumnsMap();

        for (Map<String, String> map : columns) {
            // 获取当前 Map 对象中的键值对
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String key = entry.getKey(); // 获取键
                String value = entry.getValue(); // 获取值
                System.out.println("Key: " + key + ", Value: " + value);
                if (value!=null&&!value.equals("")) {
                    columnDefinition.append(key).append(" VARCHAR(255) DEFAULT '").append(value).append("', ");

                } else {
                    columnDefinition.append(key).append(" VARCHAR(255) ,");

                }

            }
        }

        for (Map<String, String> map1 : fmColumnsMap) {
            // 获取当前 Map 对象中的键值对
            for (Map.Entry<String, String> entry : map1.entrySet()) {
                String key = entry.getKey(); // 获取键（后端列名）
                String value = entry.getValue(); // 获取值(前端列名)
                System.out.println("Key: " + key + ", Value: " + value);
                property.put(key, value);
            }
        }


        //前端的表格名称
        String tableName = createTableForm.getTableName();
        performanceTrends.setFTableName(tableName);

        //前端的表格描述
        String tableDescribe = createTableForm.getTableDescribe();
        performanceTrends.setFDescribe(tableDescribe);

        //前端的表格表格名称
        List<String> tableHeader = createTableForm.getTableHeader();
        //构造动态表格以及记录存储
        if (tableHeader.size() == 0) {
            return Result.fail("无基础列，不允许创建");
        }
        for (int i = 0; i < tableHeader.size(); i++) {
            if (tableHeader.size() == 1) {
                columnDefinition.append("_").append(i).append(" VARCHAR(255) ");
                property.put("_" + i, tableHeader.get(i));
            } else {
                if (i == tableHeader.size() - 1) {
                    columnDefinition.append("_").append(i).append(" VARCHAR(255) ");
                    property.put("_" + i, tableHeader.get(i));
                } else {
                    columnDefinition.append("_").append(i).append(" VARCHAR(255) , ");
                    property.put("_" + i, tableHeader.get(i));
                }
            }
        }
        //转json存储。
        performanceTrends.setProperty(JSON.toJSONString(property));
        performanceTrendsMapper.createTable(MTableName, columnDefinition.toString());

        performanceTrendsMapper.insert(performanceTrends);

        return Result.success("创建成功");
    }

    @Override
    public Result getTable(TableNameQuery tableNameQuery) {
//        QueryWrapper<PerformanceTrends> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("f_table_name)",tableNameQuery.getTableName());
//        List<PerformanceTrends> list = performanceTrendsMapper.selectList(queryWrapper);

        List<PerformanceTrends> list = performanceTrendsMapper.getTable(tableNameQuery);

//        List<PerformanceTrends> list = this.list();

        List<PTableVo> tableVos = new ArrayList<>();
        //小数据量，使用fori即为最高效率。
        for (int i = 0; i < list.size(); i++) {
            PTableVo pTableVo = new PTableVo();
            pTableVo.setId(list.get(i).getId());
            pTableVo.setFTableName(list.get(i).getFTableName());
            pTableVo.setFDescribe(list.get(i).getFDescribe());
            Map<String, String> map = JSON.parseObject(list.get(i).getProperty().toString(), new TypeReference<Map<String, String>>() {
            });

            // 动态管理基础列
            //获取前后的对应的列名。
            List<Map<String, String>> fmColumnsMap = baseTable.getFMColumnsMap();

//            for (Map<String, String> map1 : fmColumnsMap) {
//                // 获取当前 Map 对象中的键值对
//                for (Map.Entry<String, String> entry : map1.entrySet()) {
//                    String key = entry.getKey(); // 获取键
//                    String value = entry.getValue(); // 获取值
//                    System.out.println("Key: " + key + ", Value: " + value);
//                    map.put(key, value);
//                }
//            }

            pTableVo.setProperty(map);
            tableVos.add(pTableVo);
        }
        return Result.success(tableVos);
    }


    /**
     * 查询原型：select * from tableName where <script> 构造 like 语句进行查询 （若是没有 1=1 ）<script/>
     *
     * */
    @Override
    public Result getTableDetails(TableQuery tableQuery) {
        //构造查询条件
        Map<String, String> query = tableQuery.getCondition();

        //唯一索引值
        Integer id = tableQuery.getId();
        PerformanceTrends performanceTrends = performanceTrendsMapper.selectById(id);

        //获取表格名称
        String mTableName = performanceTrends.getMTableName();

        //构造查询条件
        String condition = "";

        //构造返回结果
        List<Map<String, Object>> list;

        for (Map.Entry<String, String> entry : query.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (value != null && !value.isEmpty()&&!key.equals("is_audit")) {
                // 拼接条件
                condition += "`" + key + "`" + " LIKE '%" + value + "%' AND ";
            }else if(value != null && !value.isEmpty()){
                condition += "`" + key + "`" + " ='" + value + "' AND ";
            }
        }
        System.out.println("构造的查询条件为：" + condition);
        // 去掉最后多余的 "AND"
        if (!condition.isEmpty()) {
            condition = condition.substring(0, condition.length() - 5);
        } else {
            condition = "1=1";
        }

        System.out.println("构造的查询条件为：" + condition);
        Page<Map<String,Object>> page;
        if(tableQuery.getCurrentPage()!=null&&!tableQuery.equals("")&&tableQuery.getPageSize()!=null&&!tableQuery.getPageSize().equals("")){
            page = new Page<>(tableQuery.getCurrentPage(),tableQuery.getPageSize());
            page = performanceTrendsMapper.getTableDetailsByPage(page,mTableName, condition);
            BaseVo<Map<String,Object>> baseVo = new BaseVo();

            baseVo.setRecords(page.getRecords());
            baseVo.setAllPages(page.getPages());
            baseVo.setAllRecordNum(page.getTotal());
            baseVo.setCurrentPage(tableQuery.getCurrentPage());
            baseVo.setPageSize(tableQuery.getPageSize());

            return Result.success(baseVo);
        }
       else {
            list = performanceTrendsMapper.getTableDetails(mTableName, condition);
            return Result.success(list);
        }

    }


    /**
     * 代码需改进的缺陷：没有校验状态是否为待审核或者是未同意的状态而让接口直接可执行。
     * （待改进）
     * */
    @Override
    public Result updateDetails(UpdateDetailsForm updateDetailsForm) {
        // 根据excelChangeForm获取tableName和修改的信息
        Map<String, Object> map = updateDetailsForm.getCondition();
        Integer tableId = updateDetailsForm.getTableId();
        Integer rowId = updateDetailsForm.getRowId();

        // 获取tableName
        PerformanceTrends byId = this.getById(tableId);
        String tableName = byId.getMTableName();

        // 拼接条件 id=rowId
        String condition = "id=" + rowId;
        // 拼接line
        StringBuilder lineBuilder = new StringBuilder();
        int i = 0;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            String temp = "`" + key + "`=" + "'" + value + "'";
            if (++i < map.size()) {
                temp += ",";
            }
            lineBuilder.append(temp);
        }
        String line = lineBuilder.toString();

        System.out.println(line);

        boolean b = performanceTrendsMapper.updateDetails(tableName, line, condition);
        if (b) {
            return Result.success("修改完成");
        }
        return Result.fail("未知错误");

    }

    @Transactional
    @Override
    public Result deletePerformanceById(String tableId) {
        PerformanceTrends byId = this.getById(tableId);
        String tableName = byId.getMTableName();
        try {
            System.out.println("删表");
            System.out.println(tableName);
            performanceTrendsMapper.deletePerformanceById(tableName);
            System.out.println("删表1");
            performanceTrendsMapper.deleteById(tableId);
            System.out.println("删表2");
        } catch (Exception e) {
            throw new RuntimeException("删除表格失败，请重试");
        }

        return Result.success("删除表格成功");
    }

    @Override
    public Result deletePerformanceDetailsById(DeleteDetailsForm deleteDetailsForm) {
        Integer tableId = deleteDetailsForm.getTableId();
        Integer rowId = deleteDetailsForm.getRowId();

        // 获取tableName
        PerformanceTrends byId = this.getById(tableId);
        String tableName = byId.getMTableName();

        // 拼接条件 id=rowId
        String condition = "id=" + rowId;

        boolean b = performanceTrendsMapper.deletePerformanceDetailsById(tableName, condition);
        if (b) {
            return Result.success("修改完成");
        }
        return Result.fail("未知错误");
    }

    @Override
    public void updateTableInfo(UpdateTableForm updateTableForm) {
        Integer id = updateTableForm.getId();
        PerformanceTrends performanceTrends = performanceTrendsMapper.selectById(id);
        Map<String, String> property=  (Map<String, String>)JSON.parse(performanceTrends.getProperty().toString());
//        Map<String, String> property = (Map<String, String>) performanceTrends.getProperty();

        Map<String, String> formProperty = updateTableForm.getProperty();

        for (Map.Entry<String, String> entry : formProperty.entrySet()) {
            String key = entry.getKey();
            if (property.containsKey(key)) {
                String value = entry.getValue();
                System.out.println("key:"+key+"   value:"+ value);
                property.put(key, value);
            }
        }
        String string = JSON.toJSONString(property);
        performanceTrends.setFTableName(updateTableForm.getFTableName());
        performanceTrends.setFDescribe(updateTableForm.getFDescribe());

        performanceTrends.setProperty(string);

        performanceTrendsMapper.updateById(performanceTrends);
    }

    @Override
    public Result addDetails(AddDetailsForm addDetailsForm) {

        Integer tableId = addDetailsForm.getTableId();

        PerformanceTrends performanceTrends = performanceTrendsMapper.selectById(tableId);
        String mTableName = performanceTrends.getMTableName();

        Map<String, Object> condition = addDetailsForm.getCondition();

        StringBuilder columns = new StringBuilder();
        StringBuilder values = new StringBuilder();

        for (Map.Entry<String, Object> entry : condition.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            // 构造表中的字段名
            columns.append(key).append(", ");

            // 根据值的类型，构造不同类型的数值
            if (value instanceof String) {
                values.append("'").append(value).append("', ");
            } else {
                values.append(value).append(", ");
            }
        }

        // 删除最后多余的逗号和空格
        columns.delete(columns.length() - 2, columns.length());
        values.delete(values.length() - 2, values.length());
        performanceTrendsMapper.addDetails(mTableName, columns, values);
        return Result.success("添加成功");
    }

    @Override
    public void UpdateStatusAgree(UpdateStatusForm updateStatusForm) {

        Integer tableId = updateStatusForm.getTableId();
        Integer rowId = updateStatusForm.getRowId();

        // 获取tableName
        PerformanceTrends byId = this.getById(tableId);
        String tableName = byId.getMTableName();

        // 拼接条件 id=rowId
        String condition = "id=" + rowId;

        String line = " is_audit = 'agree' ";

        System.out.println(line);
        try {
            performanceTrendsMapper.updateDetails(tableName, line, condition);
        }catch (Exception e){
            e.printStackTrace();
            throw new ChangeStatusException();
        }

    }

    @Override
    public void UpdateStatusDisAgree(UpdateStatusForm updateStatusForm) {
        Integer tableId = updateStatusForm.getTableId();
        Integer rowId = updateStatusForm.getRowId();

        // 获取tableName
        PerformanceTrends byId = this.getById(tableId);
        String tableName = byId.getMTableName();

        // 拼接条件 id=rowId
        String condition = "id=" + rowId;

        String line = " is_audit = 'disagree' ";

        System.out.println(line);
        try {
            performanceTrendsMapper.updateDetails(tableName, line, condition);
        }catch (Exception e){
            e.printStackTrace();
            throw new ChangeStatusException();
        }
    }
    @Override
    public Result<?> getContentsByTeacherId(String teacherId) {
        List<TeacherTableVo> results = new ArrayList();
        QueryWrapper<PerformanceTrends> queryWrapper = new QueryWrapper();
        List<PerformanceTrends> performanceTrendsList = this.performanceTrendsMapper.selectList(queryWrapper);
        for (PerformanceTrends performanceTrend : performanceTrendsList) {
            TeacherTableVo teacherTableVo =new TeacherTableVo();
            //System.out.println(performanceTrend.getMTableName());
            //String condition = "1=1";
            //System.out.println(condition);
            Map<String,String> map =JSON.parseObject(String.valueOf(performanceTrend.getProperty()),new TypeReference<Map<String,String>>(){});
            teacherTableVo.setId(performanceTrend.getId());
            teacherTableVo.setProperty(map);
            teacherTableVo.setTableDetails(performanceTrendsMapper.getTableNamesByTeacherIdWithDisagree(performanceTrend.getMTableName(),teacherId));
            teacherTableVo.setFTableName(performanceTrend.getFTableName());
            teacherTableVo.setFDescribe(performanceTrend.getFDescribe());
            results.add(teacherTableVo);

        }
        return Result.success(results);
    }
    @Override
    public Result<?> getScoreByTeacherId(String teacherId) {
           Result<List<ServiceInfoVo>> result=pointsCriteriaClient.getServiceInfoByTeacherId(teacherId);
               BigDecimal serviceInfoScore = result.getData().stream().map(ServiceInfoVo::getScore).reduce(BigDecimal.ZERO, BigDecimal::add);

               Map<String, Object> map = new HashMap<>();
               map.put("公共服务积分", serviceInfoScore);

        List<PerformanceTrends> performanceTrendsList = performanceTrendsMapper.selectList(null);
        List<Map<String, Object>> tableDetails =new ArrayList<>();

        for (PerformanceTrends performanceTrend : performanceTrendsList) {
            List<Map<String, Object>> tableNamesByTeacherId=performanceTrendsMapper.getTableNamesByTeacherId(performanceTrend.getMTableName(),teacherId);
            tableDetails.addAll(tableNamesByTeacherId);
        }
        List<String> pointCriteriaIdList =tableDetails.stream().map(detail ->(String)detail.get("points_criteria_id")).collect(Collectors.toList());
           // map.put("pointCriteriaIdList",pointCriteriaIdList);
        if (!pointCriteriaIdList.isEmpty()) {

            Result<List<PointsCriteria>> pointsCriteriaResult = pointsCriteriaClient.getPointsCriteriaByList(pointCriteriaIdList);

            if (pointsCriteriaResult.getData() != null) {
                BigDecimal pointCriteriaScore = pointsCriteriaResult.getData().stream()
                        .map(PointsCriteria::getRating)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                map.put("岗位聘任评分", pointCriteriaScore);
                map.put("总分", pointCriteriaScore.add(serviceInfoScore));
            }
        } else {
            // 如果 pointCriteriaIdList 为空，只设置 serviceInfoScore
            map.put("岗位聘任评分", BigDecimal.ZERO);
            map.put("总分", serviceInfoScore);
        }

        return Result.success(map);
    }
    private String safeGetTeacherName(String teacherId) {
        try {
            Result<String> nameResult = teacherInfoClient.getName(teacherId);
            if (nameResult != null && nameResult.getCode().equals(ResultDetail.success.getCode())){ // 确保Result类有一个表明成功与否的方法，这里假设为isSuccess()
                return nameResult.getData();
            }
        } catch (Exception e) {
            // 日志记录或适当处理异常
        }
        return null;
    }
    @Override
    public Result<?> getEachTableMaxN(Integer maxN){
        List<ScoreMaxVo> scoreMaxVoList =new ArrayList<>();
        List<PerformanceTrends> performanceTrendsList = performanceTrendsMapper.selectList(null);
        for(PerformanceTrends performanceTrends:performanceTrendsList){
            ScoreMaxVo scoreMaxVo =new ScoreMaxVo();
            scoreMaxVo.setFTableName(performanceTrends.getFTableName());
            List<String> teacherIdList =performanceTrendsMapper.getAllTeacherId(performanceTrends.getMTableName());

            Map<String,BigDecimal> teacherScores = new HashMap<>();
            for (String teacherId:teacherIdList){
                String teacherName = safeGetTeacherName(teacherId);
                if (teacherName == null) {
                    continue; // 处理异常情况，跳过没有名称的教师
                }
                List<Map<String, Object>> tableNamesByTeacherId=performanceTrendsMapper.getTableNamesByTeacherId(performanceTrends.getMTableName(),teacherId);
                List<String> pointCriteriaIdList =tableNamesByTeacherId.stream().map(detail ->(String)detail.get("points_criteria_id")).collect(Collectors.toList());
                if (!pointCriteriaIdList.isEmpty()) {
                    Result<List<PointsCriteria>> pointsCriteriaResult = pointsCriteriaClient.getPointsCriteriaByList(pointCriteriaIdList);

                    if (pointsCriteriaResult.getData() != null) {
                        BigDecimal pointCriteriaScore = pointsCriteriaResult.getData().stream()
                                .map(PointsCriteria::getRating)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                        teacherScores.put(teacherName,pointCriteriaScore);
                    }
                } else {
                    // 如果 pointCriteriaIdList 为空，只设置 serviceInfoScore
                    teacherScores.put(teacherName,BigDecimal.ZERO);
                }

            }
            // 对teacherScores按照分数进行降序排序，并取前maxN个
            Map<String, BigDecimal> maxScores = teacherScores.entrySet().stream()
                    .sorted(Map.Entry.<String,BigDecimal>comparingByValue().reversed())
                    .limit(maxN)
                    .collect(Collectors.toMap(
                            Map.Entry::getKey,
                            Map.Entry::getValue,
                            (e1, e2) -> e1, // 如果有重复的key，选择第一个
                            LinkedHashMap::new)); //

            scoreMaxVo.setEachMaxList(maxScores);
            scoreMaxVoList.add(scoreMaxVo);

        }
        return Result.success(scoreMaxVoList);
    }
    @Override
    public Result<?> getAllTableMaxN(Integer maxN){
        List<PerformanceTrends> performanceTrendsList = performanceTrendsMapper.selectList(null);
        List<String> list = new ArrayList<>();
       for (PerformanceTrends performanceTrends : performanceTrendsList){
           List<String> teacherIdList =performanceTrendsMapper.getAllTeacherId(performanceTrends.getMTableName());
           list.addAll(teacherIdList);
       }
       List<String> allTeacherIdList =list.stream().distinct().collect(Collectors.toList());
       Map<String,BigDecimal> teacherScores =new HashMap<>();
       for (String teacherId : allTeacherIdList){
           String teacherName = safeGetTeacherName(teacherId);
           if (teacherName == null) {
               continue; // 处理异常情况，跳过没有名称的教师
           }
           List<Map<String, Object>> tableDetails =new ArrayList<>();
           for (PerformanceTrends performanceTrend : performanceTrendsList) {
               List<Map<String, Object>> tableNamesByTeacherId=performanceTrendsMapper.getTableNamesByTeacherId(performanceTrend.getMTableName(),teacherId);
               tableDetails.addAll(tableNamesByTeacherId);
           }
           List<String> pointCriteriaIdList =tableDetails.stream().map(detail ->(String)detail.get("points_criteria_id")).collect(Collectors.toList());

           if (!pointCriteriaIdList.isEmpty()) {
               Result<List<PointsCriteria>> pointsCriteriaResult = pointsCriteriaClient.getPointsCriteriaByList(pointCriteriaIdList);

               if (pointsCriteriaResult.getData() != null) {
                   BigDecimal pointCriteriaScore = pointsCriteriaResult.getData().stream()
                           .map(PointsCriteria::getRating)
                           .reduce(BigDecimal.ZERO, BigDecimal::add);
                   teacherScores.put(teacherName, pointCriteriaScore);

               }
           } else {
               // 如果 pointCriteriaIdList 为空，只设置 serviceInfoScore
               teacherScores.put(teacherName, BigDecimal.ZERO);
           }
       }
        Map<String, BigDecimal> maxScores = teacherScores.entrySet().stream()
                .sorted(Map.Entry.<String,BigDecimal>comparingByValue().reversed())
                .limit(maxN)
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (e1, e2) -> e1, // 如果有重复的key，选择第一个
                        LinkedHashMap::new)); //

        return Result.success(maxScores);
        }

    @Override
    public Result<List<String>> selectPerformance(SelectQuery selectQuery) {
        List<String> resultList = new ArrayList<>();
        List<PerformanceTrends> performanceTrendsList = performanceTrendsMapper.selectList(null);
        for (PerformanceTrends performanceTrends : performanceTrendsList){
            String condition = "teacher_id = '" + selectQuery.getTeacherId() + "' " +
                    "AND is_audit = '" + selectQuery.getIsAudit() + "'";
            Map<String,String> propertyMap = JSON.parseObject(performanceTrends.getProperty().toString(), new TypeReference<Map<String, String>>() {});
            List<Map<String,Object>> performanceDetails = performanceTrendsMapper.getTableDetails(performanceTrends.getMTableName(),condition);
            propertyMap.forEach((key,value) ->
                {
                    //出错控制，避免错误的时间格式影响系统运行。
                    try{
                    if (TimeWordsList.containsTimeWord(value)){
                        for (Map<String,Object> map : performanceDetails){
                            String[] dateArray = map.get(key).toString().split("——");

                            if (dateArray.length > 1){
                                if (DateUtil.parse(dateArray[0]).compareTo(DateUtil.parse(selectQuery.getStartTime())) >= 0 &&
                                        DateUtil.parse(dateArray[1]).compareTo(DateUtil.parse(selectQuery.getEndTime())) <= 0){
                                    resultList.add(map.toString());
                                }
                            } else {
                                if (DateUtil.parse(dateArray[0]).compareTo(DateUtil.parse(selectQuery.getStartTime())) >= 0 &&
                                        DateUtil.parse(dateArray[0]).compareTo(DateUtil.parse(selectQuery.getEndTime())) <= 0){
                                    resultList.add(map.toString());
                                }
                            }
                        }
                    }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                });


        }
        return Result.success(resultList);
    }

    @Override
    public Result<PerformanceStatusVo> getPerformanceCount() {
        long count = this.count();
        PerformanceStatusVo performanceStatusVo = new PerformanceStatusVo();
        performanceStatusVo.setAllNum(count);
        QueryWrapper<PerformanceTrends> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("f_table_name","m_table_name");
        List<PerformanceTrends> list = this.list(queryWrapper);
        List<String> line1=new ArrayList<>();
        List<Long> line2=new ArrayList<>();
        List<Object> collect = list.stream().map(performanceTrends -> {
            String fTableName = performanceTrends.getFTableName();
            String mTableName = performanceTrends.getMTableName();
            Long performanceCount = this.baseMapper.getPerformanceCount(mTableName);
            line1.add(fTableName);
            line2.add(performanceCount);
            return null;
        }).collect(Collectors.toList());

        performanceStatusVo.setPerformanceName(line1);
        performanceStatusVo.setPerformanceNum(line2);
        return Result.success(performanceStatusVo);
    }


}







